﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace System.Data
{

    public class cObject : Object, IConvertible
    {
        private string _value;

        public cObject(string value)
        {
            _value = value;
        }

        public TypeCode GetTypeCode()
        {
            return this.GetTypeCode();
        }

        public bool ToBoolean(IFormatProvider provider)
        {
            return Convert.ToBoolean(_value);
        }

        public byte ToByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public char ToChar(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public DateTime ToDateTime(IFormatProvider provider)
        {
            return Convert.ToDateTime(_value, provider);
        }

        public decimal ToDecimal(IFormatProvider provider)
        {
            return Convert.ToDecimal(_value, provider);
            //throw new NotImplementedException();
        }

        public double ToDouble(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public short ToInt16(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public int ToInt32(IFormatProvider provider)
        {
            return Convert.ToInt32(_value, provider);
        }

        public long ToInt64(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public sbyte ToSByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public float ToSingle(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public string ToString(IFormatProvider provider)
        {
            if (_value == null)
            {
                return "";
            }
            return _value.ToString();
        }

        public override string ToString()
        {
            if (_value == null)
            {
                return "";
            }
            return _value.ToString();
        }

        public object ToType(Type conversionType, IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public ushort ToUInt16(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public uint ToUInt32(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public ulong ToUInt64(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }
    }


    public interface IDataReader
    {
        int FieldCount { get; }
        bool SetData(string xml);
        bool Read();
        object this[int Index]
        { get; }

        object this[string columnName]
        { get; }

        DataTable GetTable();

        void Dispose();
        bool Close();
        String GetName(int index);


        bool IsDBNull(int j);
        bool IsDBNull(string colname);

        string GetString(int p);

        decimal? GetDecimal(int p);
    }

    public class DataReader : IDataReader
    {
        private DataSet ds = null;
        private int counter = -1;

        public Object this[int index]
        {
            get
            {
                return ds.Tables[0].Rows[counter][index];
            }
        }

        public Object this[string columnName]
        {
            get
            {

                DataRow dr = ds.Tables[0].Rows[counter];
                return  dr[columnName];
            }
        }

        public bool Read()
        {
            counter += 1;
            if (ds == null || ds.Tables[0].Rows.Count <= counter)
            {
                return false;
            }


            return true;
        }

        public bool SetData(String xml)
        {
            ds = new DataSet();
            ds.FromXml(xml);
            return true;
        }

        public bool SetData(XElement xml)
        {
            ds = new DataSet();
            ds.FromXml(xml);
            return true;
        }


        public bool Close()
        {
            ds = null;
            counter = -1;
            return true;
        }

        public override string ToString()
        {
            return base.ToString();
        }

        public DataTable GetTable()
        {
            if (ds == null)
            {
                return null;
            }
            DataTable dt = ds.Tables[0];
            Close();
            return dt;
        }

        public void Dispose()
        {
            if (this.ds != null)
            {
                ds.Tables.Clear();
                ds = null;
            }
        }

        public int FieldCount
        {
            get
            {
                if (ds == null)
                { return 0; }
                else
                    return ds.Tables[0].Columns.Count;
            }
        }


        public string GetName(int index)
        {
            if (ds == null)
            {
                throw new NullReferenceException("Table is null");
            }
            if (ds.Tables[0].Columns.Count < index)
            {
                throw new ArgumentOutOfRangeException("Index is outside of column Array");
            }
            return ds.Tables[0].Columns[index].ColumnName;
        }

        public bool HasRows
        {
            get
            {
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public bool IsDBNull(int j)
        {
            if (ds != null && ds.Tables[0].Rows[counter].ItemArray.Count > j)
            {
                if (ds.Tables[0].Rows[counter].ItemArray[j].Value == null)
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsDBNull(string colName)
        {
            if (ds != null && ds.Tables[0].Columns[colName] != null)
            {
                if (ds.Tables[0].Rows[counter].ItemArray[colName].Value == null)
                {
                    return true;
                }
            }
            return false;
        }


        public string GetString(int p)
        {
            if (ds != null && ds.Tables[0].Rows[counter].ItemArray.Count > p)
            {
                if (ds.Tables[0].Rows[counter][p] == null)
                {
                    return "";
                }
                return ds.Tables[0].Rows[counter][p].ToString();
            }
            throw new IndexOutOfRangeException("Inavild column index");
        }


        public decimal? GetDecimal(int p)
        {
            if (ds != null && ds.Tables[0].Rows[counter].ItemArray.Count > p)
            {
                if (ds.Tables[0].Rows[counter][p] == null)
                {
                    return null;
                }
                return Convert.ToDecimal(ds.Tables[0].Rows[counter][p]);
            }
            throw new IndexOutOfRangeException("Inavild column index");
        }
    }
}
